﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TXTD.Admin.Data.Entitys.System;

namespace TXTD.Admin.Data
{
    /// <summary>
    /// 数据上下文
    /// </summary>
    public class TXTDAdminDbContext : DbContext
    {
        private readonly IConfiguration _configuration;
        public TXTDAdminDbContext(IConfiguration configuration)
        {
            _configuration = configuration;
        }
        public virtual DbSet<Log4Net> Log4Nets { get; set; } = null!;

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            //数据库类型
            string SqlType = _configuration["SqlDataConfig:SqlType"];
            //数据库连接字符串
            string ConnectionString = _configuration["SqlDataConfig:ConnectionString"];
            if (!optionsBuilder.IsConfigured)
            {
                optionsBuilder.UseSqlServer(ConnectionString);
            }
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            //为Oracle数据库的时候 添加数据库架构名称
            if (this.Database.IsOracle())
            {
                //配置默认的数据库架构名称。 此默认数据库架构名称用于没有显式配置架构名称的数据库对象。
                modelBuilder.HasDefaultSchema("TXTD");
            }

            //在给定程序集中应用实现 IEntityTypeConfiguration 的类型中指定的所有配置
            modelBuilder.ApplyConfigurationsFromAssembly(typeof(TXTDAdminDbContext).Assembly);
        }
    }
}
